var member;
var cardMaxCount=5;
var cardNowCount=0;
$(function(){
	//获取cookie中的对象
	member=getEntity("memberInfo");
	if(member){
		$("#mName").html("会员名："+member.mName);
		$("#mPhone").html("绑定手机："+member.mPhone+"<a href=''>修改手机</a>");
		if(member.mImg){
			$("#imgSpan").prepend('<img src="'+baseFile+member.mImg+'" id="mImg" alt="" />')
		}else{
			$("#imgSpan").prepend('<img src="'+rootPath+'/images/header.png" id="mImg" alt="" />')
		}
		$("#mId").val(member.mId);
	}else{
		window.location.href=rootPath+"/member/loginPre?redirect=/member/manage_card";
	}
	//查询畅捷通快捷支付支持的银行机构列表
	chanQPayBankInstList();
	
	$(".set_addr .right form  .other").click(function(){
			$(".step_aa").css('display','block');
	});	
	
   //关闭弹窗
	$('.step_aa .close > img').click(function(){
		$(this).parents('.step_aa').css('display','none');
	});
	
	//加载用户银行卡列表
	memberCardList();
	
	//选择银行机构
	$(document).on("click",".step_aa  ul li",function(){
		$(".m_card form img ").attr("src",$(this).children().attr("src"));
		$(".m_card form img ").attr("alt",$(this).children().attr("alt"));
		$(".m_card form img ").css({"width": "140px","height": "40px","margin":"0 10px 0 3px"});
		$("#bankName").val($(this).attr("instName"));
		$("#instId").val($(this).attr("instId"));
		$('.step_aa').css('display','none');
	});

	//监听用户银行卡号输入
	$('#cardNumber').bind('change', function() {
		return isTheChooseBankCard($(this).val());
    }); 

    //点击-确定添加
	$('.set_addr .right form').one("submit",function(e){
		e = window.event || event;
		e.preventDefault();
		var data = $("#addBankCard").serialize();
		if(!CommnUtil.notNull(data)){
			layer.msg("必填字段不能为空!",{icon:1,skin: 'layui-layer-red ',offset: ['40%', '45%']});
         	return false;
		}
	/*	if(cardMaxCount-cardNowCount<=0){
			return false;
		}*/
		var url = rootPath + '/card/addMemberCard';
		var result = CommnUtil.ajax(url, data, "json");
		if(result.resultSuccess == true ){
			layer.msg(result.resultDesc,{icon:1,skin: 'layui-layer-red ',offset: ['40%', '45%']});
			$("#addBankCard")[0].reset(); 
			//清除之前的银行卡列表
			$('.set_addr .right .address ul > .item').remove();
			//加载用户银行卡列表
			memberCardList();
		}else{			
		//	$("#cardNumber").val("");
			layer.msg(result.resultDesc,{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
		}
	});
});

//判断该银行卡号是否是所选择的的银行卡
function isTheChooseBankCard(cardNumber){
	var url = rootPath + '/card/cardIsBelongsTheBank';
	var data ={} ;
	data["cardNumber"] = cardNumber;
	data["bankName"] = $("#bankName").val();
	var result = CommnUtil.ajax(url,data, "json");
	if(result.resultSuccess == true ){
		if(result.cardIsBelongsTheBank == true){
			return true;
		}else{

			$("#cardNumber").val("");
		//	alert("卡号错误,请重新输入!");
	        layer.msg("卡号错误,请重新输入!",{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
			return false;
		}
	}else{
		$("#cardNumber").val("");
		//alert(result.resultDesc);
      layer.msg(result.resultDesc,{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});
		return false;
	}
}
//查询用户银行卡列表
function memberCardList(){
	var url = rootPath + '/card/memberCardsList';	
	var result = CommnUtil.ajax(url, 
								{"memberId":member.mId},
								"json");
	if(result.resultSuccess ==true){
		var cardList = result.data;
		cardNowCount =cardList.length;
		//$(".cardAlarm").text("");
		//$(".cardAlarm").text("已添加了"+cardNowCount+"张银行卡，还能添加"+(cardMaxCount-cardNowCount)+"张银行卡");
		$.each(cardList,function(i,item){
			var card_number = item.cardNumber;
			if(CommnUtil.notNull(card_number)){
				var subNumIndex = 4 + (card_number.length -3);
				var replaceStr="";
				var $fornumber = card_number.length - 6;
				while($fornumber > 1){
					replaceStr +="*";
					$fornumber--;
				}
				card_number = card_number.substring(0,4) + replaceStr + card_number.substring(card_number.length-3,card_number.length);
			}
			
			var phone = item.phone;
			if(CommnUtil.notNull(phone)){
				phone = phone.substring(0,3)+"****"+phone.substring(8,11);
			}
			var card = '<li class="item">'
				+'<p>'+ item.bankName +'</p>'
				+'<p class="bank_number">'+card_number+'</p>'
				+'<p class="">'+ item.realName+'</p>	'					
				+'<p>' +phone +'</p>'
				+'<p class="operate"><a onclick="selectCardById(\''+item.id+'\')"  href="javascript:void(0);">修改|</a><a onclick="deleteCardById(\''+item.id+'\')" href="javascript:void(0);" class="delete">删除</a></p>'
				+'</li>';
			$('.set_addr .right .address ul > .list').after(card);
		});
	}else{
		//alert('请求用户开户银行卡列表失败!',{icon: 2});
        layer.msg('请求用户开户银行卡列表失败!',{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});	
        Pause(this,6000);//调用暂停函数
	    this.NextStep=function(){
			window.location.href=rootPath + '/member/manage_card';
	    }
	}
}
//查询畅捷通快捷支付支持的银行机构列表
function chanQPayBankInstList(){
	var url = rootPath + '/card/bankCardList';
	var result = CommnUtil.ajax(url,{},"json");
	if(result.resultSuccess){
		var instList = result.data;
		$('.step_aa ul').empty();
		$.each(instList,function(i,item){
			var selected ="";
			if(i == 0){
				selected ="selected";
			}
			var instId = item.instId;
			var bank = '<li instId="'+ instId +'" instName ="' + item.instName  +'" ><img src="'+ item.instImg  +'" alt="'+ item.instName +'" /></li>';
			$('.step_aa ul').append(bank);
		});
	}else if(result.sessionValid){
		//alert("会话失效，请重新登录!");
      layer.msg('会话失效，请重新登录!',{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});	
      Pause(this,6000);//调用暂停函数
	    this.NextStep=function(){
			window.location.href=rootPath + '/member/loginPre?redirect=/member/manage_card';
	    }
	}else{
      layer.msg('请求用户开户银行卡列表失败!',{icon:2,skin: 'layui-layer-red ',offset: ['40%', '45%']});	
		Pause(this,6000);//调用暂停函数
	    this.NextStep=function(){
			window.location.href=rootPath + '/member/manage_card';
	    }
	}
}


//修改银行卡
function selectCardById(id){
		$.ajax({
			type:'post',
			url:rootPath + "/card/selectCardById?id="+id,
			dataType:"JSON",
			success:function(data){
				if(data.flag == true){
					var memberCard=data.memberCard;
					$("#cardId").val(memberCard.id);
					//$("#cardNumber").attr("disabled","disabled");
					$("#cardNumber").val(memberCard.cardNumber);	
					$("#realName").val(memberCard.realName);
					$("#idNumber").val(memberCard.idNumber);
					$("#phone").val(memberCard.phone);
					$("#bankName").val(memberCard.bankName);	
				}
			}
		});
	}
//删除银行卡
function deleteCardById(id){
		$.ajax({
			type:'post',
			url:rootPath + "/card/deleteCardById?id="+id,
			dataType:"JSON",
			success:function(data){
				if(data.flag == true){
					location.reload(true);
				}
			}
		})
	}
